package com.zunipe.until;

import com.zunipe.minix.MinecraftClientMixin;
import net.minecraft.client.MinecraftClient;
import net.minecraft.tag.BlockTags;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;

public class FlowerFunction
{
    public static void Flower(MinecraftClient client)
    {
        new Thread(() ->
        {
            try
            {
                if (client.player != null && client.world != null && client.cameraEntity != null)
                {
                    BlockPos pos = client.player.getBlockPos();
                    Vec3d cameraPos = client.cameraEntity.getPos();
                    float yaw = client.cameraEntity.getYaw(), pitch = client.cameraEntity.getPitch();
                    for (int i = -1; i < 3; ++i)
                    {
                        for (int j = -4; j <= 4; ++j)
                        {
                            for (int k = -4; k <= 4; ++k)
                            {
                                BlockPos blockPos = pos.add(j, i, k);
                                if (client.world.getBlockState(blockPos).isIn(BlockTags.SMALL_FLOWERS) || client.world.getBlockState(blockPos).isIn(BlockTags.TALL_FLOWERS))
                                {
                                    Vec3d off = client.world.getBlockState(blockPos).getModelOffset(client.world, blockPos);
                                    double x = blockPos.getX() + 0.5 + off.x / 2 - cameraPos.getX(),
                                            y = blockPos.getY() - cameraPos.getY() - 1.2,
                                            z = blockPos.getZ() + 0.5 + off.z / 2 - cameraPos.getZ();
                                    double r = Math.sqrt(x * x + z * z);
                                    double f = -Math.atan2(x, z);
                                    double p = -Math.atan2(y, r);
                                    client.cameraEntity.setYaw((float) (f / Math.PI * 180));
                                    client.cameraEntity.setPitch((float) (p / Math.PI * 180));
                                    Thread.sleep(20);
                                    ((MinecraftClientMixin) client).doAttackInvoker();

                                }
                            }
                        }
                    }
                    client.cameraEntity.setYaw(yaw);
                    client.cameraEntity.setPitch(pitch);
                }
            } catch (InterruptedException e)
            {
                throw new RuntimeException(e);
            }
        }).start();
    }
}
